package com.jrwd.recycletest;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.bumptech.glide.Glide;
import com.jrwd.okhttputils.OkHttpUtils;
import com.jrwd.recycletest.mange.ActivitiesManager;
import com.jrwd.recycletest.state.LoadViewHelper;
import com.jrwd.recycletest.state.VaryViewHelperX;
import com.jrwd.recycletest.util.CacheUtil;
import com.umeng.message.PushAgent;


/**
 * 相信自己，创造未来
 * @author 小兵
 * @date 2017/10/20
 */

public abstract class BaseActivity extends AppCompatActivity {

    public CacheUtil cacheUtil;

    private LoadViewHelper loadViewHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivitiesManager.createActivity(this);
        PushAgent.getInstance(this).onAppStart();
        cacheUtil = CacheUtil.get(this);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

    }

    /*************************************************************************************/
    /**
     * 初始化显示各种状态界面
     * @param container
     */
    public void initLoadingHelper(View container){
        if(loadViewHelper == null){
            loadViewHelper = new LoadViewHelper(new VaryViewHelperX(container));
        }
    }

    public void showEmptyLayout(int resId,View.OnClickListener clickListener){
        if(loadViewHelper == null){
            return;
        }
        loadViewHelper.showEmpty(resId,clickListener);
    }

    /**
     * 显示空数据view
     * @param clickListener
     */
    public void showEmptyView(View.OnClickListener clickListener){
        if(loadViewHelper == null){
            return;
        }
        loadViewHelper.showEmpty(clickListener);
    }

    /**
     * 显示空数据view
     * @param resId
     * @param clickListener
     */
    public void showEmptyView(int resId, View.OnClickListener clickListener){
        if(loadViewHelper == null){
            return;
        }
        loadViewHelper.showEmpty(clickListener, resId);
    }

    /**
     * 显示错误View
     * @param onClickListener
     */
    public void showErrorView(View.OnClickListener onClickListener){
        if(loadViewHelper == null){
            return;
        }
        loadViewHelper.showError(onClickListener);
    }

    /**
     * 显示错误View
     * @param onClickListener
     * @param resId
     */
    public void showErrorView(View.OnClickListener onClickListener, int resId){
        if(loadViewHelper == null){
            return;
        }
        loadViewHelper.showError(onClickListener, resId);
    }

    /**
     * 显示加载数据View
     */
    public void showLoadingView(){
        if(loadViewHelper == null){
            return;
        }
        loadViewHelper.showLoading(R.layout.layout_load_ing);
    }

    /**
     * 显示加载数据View
     * @param message
     */
    public void showLoadingView(String message){
        if(loadViewHelper == null){
            return;
        }
        loadViewHelper.showLoading(message);
    }

    /**
     * 显示网络无连接View
     * @param clickListener
     */
    public void showNetworkNoConnect(View.OnClickListener clickListener){
        if(loadViewHelper == null){
            return;
        }

        loadViewHelper.showNetwordNoConnect(clickListener);
    }

    /**
     * 显示网络无连接View
     * @param clickListener
     * @param resId
     */
    public void showNetworkNoConnect(View.OnClickListener clickListener, int resId){
        if(loadViewHelper == null){
            return;
        }
        loadViewHelper.showNetwordNoConnect(clickListener, resId);
    }

    /**
     * 显示数据加载成功后的界面
     */
    public void showSuccessView(){
        if(loadViewHelper == null){
            return;
        }
        loadViewHelper.restore();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
       // ImmersionBar.with(this).destroy();

      //  LoadingDialog.dismissDialog();
        OkHttpUtils.getInstance().cancelTag(this);
        ActivitiesManager.removeActivity(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        Glide.get(this).clearMemory(); //当被压栈时回收缓存,释放资源.
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }


}
